import 'package:flutter/material.dart';
import 'package:znmm/UI/A_Common/buttons/easy_button/easy_elevated_button.dart';
import 'package:znmm/UI/A_Common/custom_textField.dart';
import 'package:znmm/config/app_color.dart';
import '../utils/my_global.dart';
import 'mag/chat_msg_manage.dart';
import 'model/chat_msg_model.dart';
import 'view/chat_controller.dart';
import 'view/chat_list_view.dart';

//import 'manage/PTGpt_manage.dart';

class ChatPage extends StatefulWidget {
  const ChatPage({Key? key}) : super(key: key);

  @override
  ChatPageState createState() => ChatPageState();
}

class ChatPageState extends State<ChatPage> {
  final GlobalKey<CustomTextFieldState> _glKey =
      GlobalKey<CustomTextFieldState>();
  late ChatController chatController;

  @override
  void initState() {
    super.initState();
    chatController = ChatController(
        initialMessageList: ChatMsgManager().getYandexList(),
        timePellet: 60,
        scrollController: ScrollController());

    //PTGTPBtnManager.hideButton();
  }

  @override
  void dispose() {
    //PTGTPBtnManager.showAgain();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: MyGlobal.getNavigationBar(context, "Chat"),
        backgroundColor: AppColor.backgroundColor,
        body: Column(
          children: [
            Expanded(
                child: ChatListView(
              chatController: chatController,
            )),
            const SizedBox(
              height: 30,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                const SizedBox(
                  width: 10,
                ),
                Expanded(
                    child: CustomTextField(
                  key: _glKey,
                  labelText: "Mesage ChatGPT...",
                  onInputComplete: (value) {
                    //print(value);
                  },
                )),
                const SizedBox(
                  width: 10,
                ),
                EasyElevatedButton(
                  label: 'Send',
                  //color: Colors.blue,
                  onPressed: _send,
                ),
                const SizedBox(
                  width: 10,
                ),
              ],
            ),
            const SizedBox(
              height: 30,
            )
          ],
        ));
  }

  void _send() {
    String strSenMsg = "";

    if (_glKey.currentState != null) {
      strSenMsg = _glKey.currentState!.getText();
    }

    chatController.addMessage(ChatMsgModel(
        content: strSenMsg,
        ownerType: OwnerType.sender,
        createdAt: DateTime.now().millisecondsSinceEpoch,
        ownerName: "Jack"));
    Future.delayed(const Duration(milliseconds: 1000), () {
      chatController.addMessage(
        ChatMsgModel(
            content: "I'm just a copy",
            createdAt: DateTime.now().millisecondsSinceEpoch,
            ownerType: OwnerType.receiver,
            ownerName: "ChatGPT"),
      );
    });
  }
}
